package com.gjy.mongod.controller;

import com.gjy.mongod.domain.AjaxResult;
import com.gjy.mongod.domain.Book;
import com.gjy.mongod.repository.BookRepository;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-15 10:06:24
 */
@RestController
@RequestMapping("/crud")
public class CurdController {

    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private BookRepository bookRepository;

    @PostMapping("/insertBook")
    public AjaxResult insertBook(@RequestBody Book book) {
        Book save = bookRepository.save(book);
        return AjaxResult.builder().data(save).build();
    }

    @PostMapping("/findBook")
    public AjaxResult findBook(@RequestParam Long id) {
        Optional<Book> byId = bookRepository.findById(id);
        return AjaxResult.builder().data(byId).build();
    }
}
